package com.example.miaosha.common.exception;

import com.example.miaosha.entity.base.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.message.AuthException;

/**
 * 全局异常处理器
 *
 * @Author
 * @Date 2021/12/30 16:20
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(AuthException.class)
    public BaseResponse handleException(AuthException e) {
        return BaseResponse.error("程序出现了用户认证错误,错误信息为：" + e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(RocketMQException.class)
    public BaseResponse handleException(RocketMQException e) {
        return BaseResponse.error("RocketMQ出现了异常,异常信息为: " + e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(LoginException.class)
    public BaseResponse handleException(LoginException e) {
        return BaseResponse.error("登录出现了异常,异常信息为: " + e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public BaseResponse handleException(BusinessException e) {
        return BaseResponse.error("系统业务异常信息：" + e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public BaseResponse handleException(Exception e) {
        log.warn(e.getLocalizedMessage());
        return BaseResponse.error("系统异常信息: " + e.getMessage());
    }
}
